package cn.panda.edu.controller;

import cn.panda.edu.model.Teacher;
import cn.panda.edu.model.TeacherWithBLOBs;
import cn.panda.edu.service.TeacherService;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.MathUtil;
import cn.panda.edu.util.ResultDataUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value="/api/admin")
@Api(description="教师管理")
public class ManageTeacherController {

	@Autowired
	private TeacherService teacherService;
	
	/**
	* @Title: TeachermanagerFy 
	* @Description: TODO(获取教师列表 分页 + 模糊查询) 
	* @param teacherId
	* @param teacherRealname
	* @param pageIndex
	* @return
	* PageInfo<Teacher>
	 */
	@ApiOperation(value="获取教师列表 分页" , notes="分页+模糊查询")//用以指明api名称 
	@ApiImplicitParams({@ApiImplicitParam(name="teacherId", value="教师ID", required=false, dataType="int", paramType="form"),
						@ApiImplicitParam(name="pageIndex", value="当前页码", required=true, dataType="int", paramType="form"),
						@ApiImplicitParam(name="teacherRealname", value="教师姓名", required=false, dataType="string", paramType="form")})
	@RequestMapping(value="TeachermanageFy",method=RequestMethod.POST)
	public Object TeachermanageFy(Integer teacherId,String teacherRealname,@RequestParam(required=true)Integer pageIndex){
		 PageInfo<Teacher> tPage = teacherService.selectByPageAndSelective(teacherId, teacherRealname, pageIndex, Constant.PAGE_SIZE);
		return ResultDataUtil.resData(200, "", tPage);
	}
	
	/**
	* @Title: TeachermanageQx 
	* @Description: TODO(修改教师权限) 
	* @param roleId
	* @param teacherId
	* @return
	* Map<String,Object>
	 */
	@ApiOperation(value="修改教师权限")
	@ApiImplicitParams({@ApiImplicitParam(name="teacherId",value="教师ID",required=true,dataType="int", paramType="form"),
						@ApiImplicitParam(name="roleId",value="角色ID",required=true,dataType="int", paramType="form")})
	@RequestMapping(value="TeachermanageQx",method=RequestMethod.POST)
	public Map<String, Object> TeachermanageQx(@RequestParam(required=true)Integer roleId, @RequestParam(required=true)Integer teacherId ) {
		Map<String, Object> map = new HashMap<>();
		int result = teacherService.updatePermissionByPrimaryKey(roleId, teacherId);
		if (result > 0) {
			map = ResultDataUtil.resData(200, "修改成功",null) ;
		}else {
			map = ResultDataUtil.resData(500, "修改失败",null) ;
		}
		return map;
		
	}
	
	/**
	* @Title: TeachermanageAdd 
	* @Description: TODO(添加教师) 
	* @param teacher
	* @return
	* Map<String,Object>
	 */
	@ApiOperation(value="添加教师")
	@ApiImplicitParam(name="teacher",value="教师",required=false,dataType="TeacherWithBLOBs", paramType="form")
	@RequestMapping(value="TeachermanageAdd",method=RequestMethod.POST)
	public Map<String, Object> TeachermanageAdd(TeacherWithBLOBs teacher){
		Map<String, Object> map = new HashMap<>();
		int result = teacherService.insertSelective(teacher);
		if (result > 0) {
			map = ResultDataUtil.resData(200, "添加成功",null) ;
		}else {
			map = ResultDataUtil.resData(500, "添加失败",null) ;
		}
		return map;
	}
	
	/**
	* @Title: TeachermanageDel 
	* @Description: TODO(删除教师) 
	* @param teacherId
	* @return
	* Map<String,Object>
	 */
	@RequestMapping(value="TeachermanageDel",method=RequestMethod.POST)
	@ApiOperation(value="删除教师")
	@ApiImplicitParam(name="teacherId",value="教师ID",required=true,dataType="int", paramType="form")
	public Map<String, Object> TeachermanageDel(@RequestParam(required=true)Integer teacherId){
		Map<String, Object> map = new HashMap<>();
		int result = teacherService.deleteByPrimaryKey(teacherId);
		if (result > 0) {
			map = ResultDataUtil.resData(200, "删除成功",null) ;
		}else {
			map = ResultDataUtil.resData(500, "删除失败",null) ;
		}
		return map;
	}
	
	/**
	* @Title: TeachermanageSel 
	* @Description: TODO(通过ID查找教师) 
	* @param teacherId
	* @return
	* Map<String,Object>
	 */
	@RequestMapping(value="TeachermanageSel",method=RequestMethod.POST)
	@ApiOperation(value="通过ID查找教师")
	@ApiImplicitParam(name="teacherId",value="教师ID",dataType="String", paramType="form")
	public Map<String, Object> TeachermanageSel(String teacherId,HttpSession session){
		
		if(teacherId == null && "".equals(teacherId)) {
			Teacher tea = (Teacher) session.getAttribute(Constant.ADMIN_LOGIN_USER);
			teacherId = tea.getTeacherId().toString();
		}
		System.out.println(teacherId);
		TeacherWithBLOBs teacher = teacherService.selectByPrimaryKey(Integer.valueOf(teacherId));
		return ResultDataUtil.resData(200, "", teacher);
	}
	
	/**
	* @Title: TeachermanageUp 
	* @Description: TODO(通过ID修改教师) 
	* @param teacher
	* @return
	* Map<String,Object>
	 */
	@RequestMapping(value="TeachermanageUp",method=RequestMethod.POST)
	@ApiOperation(value="修改教师")
	@ApiImplicitParam(name="teacherId",value="通过ID修改教师",required=false,dataType="TeacherWithBLOBs", paramType="form")
	public Map<String, Object> TeachermanageUp(TeacherWithBLOBs teacher){
		System.out.println(teacher);
		if (teacher.getTeacherPassword() != null) {
			teacher.setTeacherPassword(MathUtil.getMD5(teacher.getTeacherId().toString()+"#"+teacher.getTeacherPassword()));
		}
		Map<String, Object> map = new HashMap<>();
		int result = teacherService.updateByPrimaryKeySelective(teacher);
		if (result > 0) {
			map = ResultDataUtil.resData(200, "修改成功",null) ;
		}else {
			map = ResultDataUtil.resData(500, "修改失败",null) ;
		}
		return map;
	}
}
